package com.admin.socketmultiplayer.activity

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.BmobUser
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import cn.bmob.v3.listener.UpdateListener
import com.admin.socketmultiplayer.R
import com.admin.socketmultiplayer.config.DataUtils
import com.admin.socketmultiplayer.config.FixedValue
import kotlinx.android.synthetic.main.activity_forgetpassword.*
import kotlinx.android.synthetic.main.layout_title.*
import org.jetbrains.anko.toast

/**
 * Created by 吴昶 on 2018/5/8.
 */
class ForgetPasswortActivity:BaseActivity(){
    override fun bindLayout(): Int {
        return R.layout.activity_forgetpassword
    }

    override fun initData(){
        iv_back.setImageResource(R.mipmap.return_normal)
        tv_title.text="找回密码"
    }

    override fun initEvent(){
        iv_back.setOnClickListener { finish() }
        btn_send_email.setOnClickListener {
            var email=edt_email.text.toString()
            if(email==""||!DataUtils.isPhoneNumRight(email,FixedValue.EAMIL)){
                toast("请输入正确的邮箱")
            }else{
                var query=BmobQuery<BmobUser>()
                query.addWhereEqualTo("email",email)
                query.findObjects(object :FindListener<BmobUser>(){
                    override fun done(p0: MutableList<BmobUser>?, p1: BmobException?) {
                        if(p0!=null&&p0.size>0){
                            BmobUser.resetPasswordByEmail(email,object :UpdateListener(){
                                override fun done(p0: BmobException?) {
                                    p0?:let {
                                        toast("已发送邮件至 邮箱 "+email+"请查收")
                                        finish()
                                    }
                                    p0?.let { toast(p0.message.toString()) }
                                }
                            })
                        }else{
                            toast("此邮箱尚未注册")
                        }
                    }
                })



            }
        }
    }
}